<template>
  <div>
    <el-dialog title="请签名" class="JNPF-dialog JNPF-dialog_center sign-dialog" :closeOnClickModal='false'
      :visible.sync="signVisible" append-to-body width="600px" :fullscreen="device == 'mobile'">
      <div class="sign-main-box">
        <vue-esign ref="esign" :lineWidth="lineWidth" />
        <div class="tip" v-show="showTip">请在此区域签名</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleReset">清空</el-button>
        <el-button type="primary" :loading="loading" @click="handleGenerate()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import vueEsign from 'vue-esign'
import { mapGetters } from 'vuex'
import { uploadImgApi } from "@/api/after/serve";

export default {
  name: 'SignImgDialog',
  components: { vueEsign },
  computed: {
    ...mapGetters([
      'device'
    ]),
  },
  props: {
    lineWidth: {
      required: true,
      type: Number
    },
    isDefault: {
      required: true,
      type: Number
    },
    userInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    type: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      signVisible: false,
      loading: false,
      signImg: '',
      showTip: true
    }
  },
  methods: {
    init() {
      this.handleReset()
      this.signVisible = true
      this.showTip = true
      this.$nextTick(() => {
        this.$watch(
          () => {
            return this.$refs.esign.hasDrew
          },
          (val) => {
            this.showTip = !val
          }
        )
      })
    },
    handleReset() {
      this.signImg = ''
      this.$nextTick(() => {
        this.$refs.esign && this.$refs.esign.reset()
      })
    },
    base64ImgtoFile(dataurl, fileName = 'file') {
      const arr = dataurl.split(',')
      const mime = arr[0].match(/:(.*?);/)[1]
      const suffix = mime.split('/')[1]
      const bstr = atob(arr[1])
      let n = bstr.length
      const u8arr = new Uint8Array(n)
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }
      return new File([u8arr], `${fileName}.${suffix}`, { type: mime })
    },
    handleGenerate() {
      const _this = this
      this.$refs.esign.generate().then(res => {
        const file = this.base64ImgtoFile(res)

        _this.$emit("close", { res, file })

        // const file = this.base64ImgtoFile(res)
        // const formData = new FormData()
        // formData.append('file', file)
        // uploadImgApi(formData).then(res => {
        //   _this.$emit("close", res)
        // }).catch(() => {
        //   this.$message.error("上传失败")
        // })

      }).catch(err => {
        this.loading = false
        this.$message.warning("请签名")
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.sign-dialog {
  >>>.el-dialog__body {
    overflow: hidden;
    height: 320px;
    overflow: auto;
    overflow-x: hidden;
    padding: 23px 14px 2px !important;
  }
}

.sign-main-box {
  border: 1px solid rgb(224, 238, 238);
  width: 100%;
  height: auto;
  background-color: rgb(247, 247, 247);
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: -10px;
  margin-bottom: -10px;
  position: relative;

  .tip {
    text-align: center;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    color: #9d9d9f;
    font-size: 16px;
    pointer-events: none;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
